<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right: 30px">
      <el-form-item label="名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="数字：" prop="number">
        <el-input v-model="form.number" oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="只能输入正整数"></el-input>
      </el-form-item>
      <el-form-item label="选择器：" prop="select">
        <el-select v-model="form.choose" placeholder="请选择" clearable>
          <el-option v-for="item in selectData" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单选框：" prop="radio">
        <el-radio-group v-model="form.radio">
          <el-radio v-for="item in radioData" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<script lang="ts" setup>
import type {LayerType} from "@/components/layer/index.vue";
import Layer from "@/components/layer/index.vue";
import {Ref, ref} from "vue";
import type {ElFormItemContext} from "element-plus/lib/el-form/src/token";
import {update} from "@/api/table";
import {radioData, selectData} from "./enum";
import {ElMessage} from "element-plus";

const props = defineProps({
  layer: {
    type: Object,
    default() {
      return {
        show: false,
        title: "",
        showButton: true,
      };
    },
  },
});
const ruleForm: Ref<ElFormItemContext | null> = ref(null);
const layerDom: Ref<LayerType | null> = ref(null);
const emits = defineEmits(["getTableData"]);
let form = ref({
  name: "",
});
const rules = {
  name: [{required: true, message: "请输入姓名", trigger: "blur"}],
  number: [{required: true, message: "请输入数字", trigger: "blur"}],
  choose: [{required: true, message: "请选择", trigger: "blur"}],
  radio: [{required: true, message: "请选择", trigger: "blur"}],
};
init();

function init() {
  // 用于判断新增还是编辑功能
  if (props.layer.row) {
    form.value = JSON.parse(JSON.stringify(props.layer.row)); // 数量量少的直接使用这个转
  } else {
  }
}

const submit = () => {
  ruleForm.value.validate((valid: boolean) => {
    if (valid) {
      let params = form.value;
      if (props.layer.row) {
        updateForm(params);
      } else {
        addForm(params);
      }
    } else {
      return false;
    }
  });
};
// 新增提交事件
const addForm = (params: object) => {
  update(params).then((res) => {
    ElMessage({
      type: "success",
      message: "新增成功",
    });
    props.layer.show = false;
    emits("getTableData", true);
  });
};

// 编辑提交事件
const updateForm = (params: object) => {
  update(params).then((res) => {
    ElMessage({
      type: "success",
      message: "编辑成功",
    });
    emits("getTableData", false);
  });
};
</script>

<style lang="scss" scoped>
</style>